package builder.demo2;

public class Computer {
    private String cpu; // cpu
    private String graphics; // 显卡
    private String power; // 电源
    private String disk; // 磁盘

    // 私有构造
    private Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.graphics = builder.graphics;
        this.power = builder.power;
        this.disk = builder.disk;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", graphics='" + graphics + '\'' +
                ", power='" + power + '\'' +
                ", disk='" + disk + '\'' +
                '}';
    }

    public static final class Builder{
        private String cpu; // cpu
        private String graphics; // 显卡
        private String power; // 电源
        private String disk; // 磁盘

        public Builder cpu(String cpu) {
            this.cpu = cpu;
            return this;
        }
        public Builder graphics(String graphics) {
            this.graphics = graphics;
            return this;
        }
        public Builder power(String power) {
            this.power = power;
            return this;
        }
        public Builder disk(String disk) {
            this.disk = disk;
            return this;
        }

        // 外部类构造函数私有化，必须在构建者中提供创建外部类实例化的方法
        // 使用构建者创建 Computer 对象
        public Computer build(){
            // 内部类中可以访问外部类中的私有方法
            return new Computer(this);
        }
    }
}